package com.meituan.catering.management.shop.biz.service.impl;

import com.meituan.catering.management.common.model.biz.PageBO;
import com.meituan.catering.management.shop.biz.model.ShopBO;
import com.meituan.catering.management.shop.biz.model.converter.PageBOConverter;
import com.meituan.catering.management.shop.biz.model.converter.ShopBOConverter;
import com.meituan.catering.management.shop.biz.model.request.*;
import com.meituan.catering.management.shop.biz.service.ShopBizService;
import com.meituan.catering.management.shop.dao.converter.PageDOConverter;
import com.meituan.catering.management.shop.dao.converter.ShopDOConverter;
import com.meituan.catering.management.shop.dao.mapper.ShopMapper;
import com.meituan.catering.management.shop.dao.model.ShopDO;
import com.meituan.catering.management.shop.dao.model.request.SearchShopDataRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * {@link ShopBizService}的核心实现
 */
@Service
public class ShopBizServiceImpl implements ShopBizService {

    @Resource
    private ShopMapper shopMapper;

    @Override
    public ShopBO findByBusinessNo(Long tenantId, Long userId, String businessNo) {
        return ShopBOConverter.toShopBO(shopMapper.findByBusinessNo(tenantId, userId, businessNo));
    }

    @Override
    public ShopBO create(Long tenantId, Long userId, SaveShopBizRequest saveShopBizRequest) {
        ShopDO shopDO = ShopDOConverter.toSaveShopDO(tenantId, userId, saveShopBizRequest);
        int id = shopMapper.insert(shopDO);
        if (id != 1) {
            return null;
        }
        return ShopBOConverter.toShopBO(shopMapper.selectById(shopDO.getId()));
    }

    @Override
    public PageBO searchShopPage(Long tenantId, Long userId, SearchShopBizRequest request) {
        SearchShopDataRequest dataRequest = PageDOConverter.toPageDO(request);
        int countAll = shopMapper.countAll(dataRequest);
        if (countAll == 0) {
            return null;
        }
        List<ShopDO> list = shopMapper.searchAll(dataRequest);
        return PageBOConverter.toPageBO(list, request, countAll);
    }

    @Override
    public ShopBO update(Long tenantId, Long userId, String businessNo, UpdateShopBizRequest updateShopBizRequest) {
        ShopDO shopDO = ShopDOConverter.toUpdateShopDO(tenantId, userId, businessNo, updateShopBizRequest);
        int updateRowCount = shopMapper.update(businessNo, shopDO);
        if (updateRowCount != 1) {
            return null;
        }
        return findByBusinessNo(tenantId, userId, businessNo);
    }

    @Override
    public ShopBO open(Long tenantId, Long userId, String businessNo, OpenShopBizRequest openShopBizRequest) {
        ShopDO shopDO = ShopDOConverter.toOpenShopDO(tenantId, userId, businessNo, openShopBizRequest);
        int openRowCount = shopMapper.open(businessNo, shopDO);
        if (openRowCount != 1) {
            return null;
        }
        return findByBusinessNo(tenantId, userId, businessNo);
    }

    @Override
    public ShopBO close(Long tenantId, Long userId, String businessNo, CloseShopBizRequest closeShopBizRequest) {
        ShopDO shopDO = ShopDOConverter.toCloseShopDO(tenantId, userId, businessNo, closeShopBizRequest);
        int closeRowCount = shopMapper.close(businessNo, shopDO);
        if (closeRowCount != 1) {
            return null;
        }
        return findByBusinessNo(tenantId, userId, businessNo);
    }
}
